{% extends "tasks/base.html" %}

{# @@@ commonalities between this and task_list.html might need to be factored out #}

{% load i18n %}
{% load account_tags %}
{% load shorttimesince_tag %}
{% load order_by %}
{% load humanize %}
{% load tasks_tags %}
{% load tagging_tags %}
{% load group_tags %}

{#}{% block head_title %}Tasks for {{ project.name }}{% endblock %}#}
{% block head_title %}Tasks{% endblock %}

{% block body %}
    <p><a href="{% groupurl task_list group %}?group_by={{ field }}">&larr; {% trans "Back to All Tasks" %}</a></p>
    
    {#<h1>{% trans "Tasks for Project" %} <a href="@@@">{{ group.name }}</a></h1> #}
    <h1>Tasks with {{ field }} = {{ value }}</h1>
    
    {% if task_filter %}
        <form action="" method="GET">
            <div class="task_filter">
                <ul>
                    {{ task_filter.form.as_ul }}
                    <li><input type="submit" value="filter"/></li>
                </ul>
            </div>
            {% if group_by %}
                <input type="hidden" name="group_by" value="{{ group_by }}" />
            {% endif %}
        </form>
    {% endif %}
    
    <p>Group by:
        {% ifequal group_by "state" %}
            <a href="?{% if gbqs %}{{ gbqs }}&{% endif %}group_by=modified">last modified</a>
            or
            <b>state</b>
            or
            <a href="?{% if gbqs %}{{ gbqs }}&{% endif %}group_by=assignee">assignee</a>
            or
            <a href="?{% if gbqs %}{{ gbqs }}&{% endif %}group_by=tag">tag</a>
            
            {% order tasks by state %}
            {% regroup tasks by get_state_display as grouped_tasks %}
        {% else %}
            {% ifequal group_by "assignee" %}
                <a href="?{% if gbqs %}{{ gbqs }}&{% endif %}group_by=modified">last modified</a>
                or
                <a href="?{% if gbqs %}{{ gbqs }}&{% endif %}group_by=state">state</a>
                or
                <b>assignee</b>
                or
                <a href="?{% if gbqs %}{{ gbqs }}&{% endif %}group_by=tag">tag</a>
                
                {% order tasks by assignee %}
                {% regroup tasks by assignee as grouped_tasks %}
            {% else %}
                {% ifequal group_by "tag" %}
                    <a href="?{% if gbqs %}{{ gbqs }}&{% endif %}group_by=modified">last modified</a>
                    or
                    <a href="?{% if gbqs %}{{ gbqs }}&{% endif %}group_by=state">state</a>
                    or
                    <a href="?{% if gbqs %}{{ gbqs }}&{% endif %}group_by=assignee">assignee</a>
                    or
                    <b>tag</b>
                    {# do no manipulation here as we'll special case below #}
                {% else %}
                    <b>last modified</b>
                    or
                    <a href="?{% if gbqs %}{{ gbqs }}&{% endif %}group_by=state">state</a>
                    or
                    <a href="?{% if gbqs %}{{ gbqs }}&{% endif %}group_by=assignee">assignee</a>
                    or
                    <a href="?{% if gbqs %}{{ gbqs }}&{% endif %}group_by=tag">tag</a>
                    
                    {% order tasks by -modified %}
                    {% regroup tasks by modified.date as grouped_tasks %}
                {% endifequal %}
            {% endifequal %}
        {% endifequal %}
    </p>
    
    <p class="expand_collapse_all"><a class="expand_all">expand all</a> &ndash; <a class="collapse_all">collapse all</a></p>
    <table class="task_list">
        <tr>
            <th>ID</th>
            <th>Summary</th>
            <th>Last Modified</th>
            <th>Assignee</th>
            <th>Status</th>
        </tr>
        {% ifequal group_by "tag" %}
            {% tags_for_model tasks.Task as task_tags %}
            {% for section in task_tags %}
                {% tasks_for_tag section as tagtasks in tasks %}
                {% if tagtasks %}
                <tbody><tr class="task_grouping">
                    <td colspan="5">
                        <span class="toggle">
                            <span class="arrow">&#x25BE;</span>
                            {{ section }}
                            <span class="count">({{ tagtasks|length }})</span>
                        </span>
                    </td>
                </tr></tbody>
                <tbody class="task_group" id="group_{{ forloop.counter }}">
                    {% for item in tagtasks %}
                        <tr class="task_{{ item.get_state_display }} {% cycle odd,even %}">
                            <td>{{ item.object.id }}</td>
                            <td><a href="{% groupurl task_detail group id=item.id %}">{{ item.summary }}</a></td>
                            <td style="white-space: nowrap">{{ item.modified|shorttimesince }} ago</td>
                            <td>
                                {% if item.assignee %}
                                    {% user_display item.assignee %}
                                {% else %}
                                    <span class="warning">unassigned</span>
                                {% endif %}
                            </td>
                            <td>{{ item.status }}</td>
                        </tr>
                    {% endfor %}
                </tbody>
                {% endif %}
            {% endfor %}
        {% else %}
            {% for section in grouped_tasks %}
                <tbody><tr class="task_grouping">
                    <td colspan="5">
                        <span class="toggle">
                            <span class="arrow">&#x25BE;</span>
                            {% ifequal group_by "state" %}
                            {{ section.grouper }}
                            {% else %}
                                {% ifequal group_by "assignee" %}
                                    {% if section.grouper %}{{ section.grouper }}{% else %}unassigned{% endif %}
                                {% else %}
                                    {{ section.grouper|naturalday:_("MONTH_DAY_FORMAT")|capfirst }}
                                {% endifequal %}
                            {% endifequal %}
                            <span class="count">({{ section.list|length }})</span>
                        </span>
                    </td>
                </tr></tbody>
                <tbody class="task_group" id="group_{{ forloop.counter }}">
                    {% for task in section.list %}
                        <tr class="task_{{ task.get_state_display }} {% cycle odd,even %}">
                            <td>{{ task.id }}</td>
                            <td><a href="{% groupurl task_detail group id=task.id %}">{{ task.summary }}</a></td>
                            <td style="white-space: nowrap">{{ task.modified|shorttimesince }} ago</td>
                            <td>
                                {% if task.assignee %}
                                    {% user_display task.assignee %}
                                {% else %}
                                    <span class="warning">unassigned</span>
                                {% endif %}
                            </td>
                            <td>{{ task.status }}</td>
                        </tr>
                    {% endfor %}
                </tbody>
            {% endfor %}
        {% endifequal %}
    </table>
    <p class="expand_collapse_all"><a class="expand_all">expand all</a> &ndash; <a class="collapse_all">collapse all</a></p>
    
{% endblock %}
